package com.pingan.haofang.searchcloud.batch.monitor;

import java.util.Calendar;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import com.pingan.haofang.searchcloud.batch.common.Batch;
import com.pingan.haofang.searchcloud.batch.common.BatchResult;
import com.pingan.haofang.searchcloud.batch.common.BatchRunner;
import com.pingan.haofang.searchcloud.monitor.service.DashboardIndexSummaryInfoService;

/**
 * 
 * 索引构建信息汇总
 * 
 * @author yangbaohu662
 * @date   2018年10月31日
 * @since  1.0.0
 */
@Batch(value = "indexSummaryInfoBuildTask")
public class IndexSummaryInfoBuildRunner implements BatchRunner {

    @Autowired
    private DashboardIndexSummaryInfoService indexSummaryInfoService;

    @Override
    public BatchResult run(String[] args) {
        System.out.println(new Date());

        Date[] rangeTime = getRangeTime();
        indexSummaryInfoService.buildIndexSummaryInfo(rangeTime[0], rangeTime[1]);

        System.out.println(new Date());
        return BatchResult.ALL_SUCCESS;
    }

    public Date[] getRangeTime() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, -1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Date fromTime = c.getTime();

        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        Date toTime = c.getTime();

        return new Date[] { fromTime, toTime };
    }

}
